package com.mulanweb.api.module.controller.admin;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.mulanweb.api.module.controller.BaseController;
import com.mulanweb.api.module.model.*;
import com.mulanweb.db.common.Constant;
import com.mulanweb.db.common.SQLGenerateHelper;
import com.mulanweb.validator.annotation.ValidateForm;

import java.util.*;

/**
 * Created by Administrator on 2016/11/8 0008.
 */
public class VisaOrderController extends BaseController {

    @ValidateForm
    public void search() {
        SQLGenerateHelper helper = new SQLGenerateHelper("`visa_order`", "create_time desc");
        helper.addEqualsCondition("status_id", getParaToInt("statusId"));
        helper.addEqualsCondition("order_number", getPara("orderNumber"));
        helper.addEqualsCondition("phone_number", getPara("phoneNumber"));
        helper.addEqualsCondition("name", getPara("name"));
        String nickname = getPara("wechatNickname");
        List<Long> userIds = new ArrayList<>();
        if (StrKit.notBlank(nickname)) {
            List<User> userList = User.dao.find("SELECT * FROM `user` WHERE wechat_nickname LIKE ?","%"+nickname+"%");
            for (User user : userList) {
                userIds.add(user.getId());
            }
        }
        helper.addInConditionToLong("user_id",userIds);
        Page<VisaOrder> page = VisaOrder.dao.paginate(getParaToInt("PageNumber"), getParaToInt("PageSize"), helper.getSqlSelect(), helper.getSqlExceptSelect(), helper.getQueryParas());
        returnPageModelJson(page);
    }

    @ValidateForm
    public void getDetail() throws Exception {
        VisaOrder visaOrder = VisaOrder.dao.findById(getPara("orderId"));
        if (visaOrder != null) {
            if (visaOrder != null) {
                Map<String, Object> listDate = new HashMap<String, Object>();
                Map<String, Object> commodity = new HashMap<String, Object>();

                VisaChild visaChild = VisaChild.dao.findById(visaOrder.getVisaChildId());
                commodity.put("visaChild",visaChild);
                commodity.put("visa",Visa.dao.findById(visaChild.getParentId()));

                listDate.put("commodity", commodity);
                listDate.put("order", visaOrder);
                listDate.put("user", User.dao.findById(visaOrder.getUserId()));
                renderJson(listDate);
            } else {
                returnFailureJson();
            }
        } else {
            returnFailureJson();
        }
    }

    public void getOperationOrder() {
        List<VisaOrder> list = VisaOrder.dao.find("SELECT * FROM `visa_order` WHERE status_id=4 AND admin_id IS NULL ORDER BY create_time desc");
        List<Object> result = new ArrayList<Object>();
        for (VisaOrder order : list) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("id", order.getId());
            map.put("orderNumber", order.getOrderNumber());
            map.put("createTime", order.getCreateTime());
            map.put("tag",true);
            result.add(map);
        }
        renderJson(result);
    }

    public void updateOrder() {
        String orderId = getPara("orderId");
        VisaOrder order = VisaOrder.dao.findById(orderId);
        Integer adminId = getSessionAttr("adminId");
        if (order != null && adminId != null) {
            order.setAdminId(adminId); //管理员接下此订单
            order.setStatusId(23L);//状态更改为 正在处理中
            if (order.update()) {
                OperationHistory operationHistory = new OperationHistory();
                operationHistory.setAdminId(adminId);
                operationHistory.setTargetId(Long.parseLong(order.getId().toString()));
                operationHistory.setTableName("visa_order");
                operationHistory.setTypeId(34L);
                operationHistory.setCreateTime(new Date());
                operationHistory.save();
                returnSuccessJson();
            }else{
                returnFailureJson();
            }
        } else {
            returnFailureJson();
        }
    }

    /**
     * 管理员完成订单，改变状态
     * 只允许接单的管理员改变
     */
    public void doneOrder(){
        String orderId = getPara("orderId");
        VisaOrder order = VisaOrder.dao.findById(orderId);
        Integer adminId = getSessionAttr("adminId");
        if (order != null && adminId != null) {
            if(order.getAdminId() != adminId){
                returnJson(0,"您无权限限改变此订单状态");
            }
            order.setStatusId(24L);//状态更改为 已完成
            if (order.update()) {
                returnSuccessJson();
            }else{
                returnFailureJson();
            }
        } else {
            returnFailureJson();
        }
    }
}
